﻿using SMS_Agent.BLL;
using SMS_Agent.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SMS_Agent.View
{
    public partial class frmLogin : Form
    {
        User_BLL userBLL;

        public frmLogin()
        {
            InitializeComponent();
            userBLL = new User_BLL();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {

        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            //Application.Exit();
            this.DialogResult = System.Windows.Forms.DialogResult.Abort;
        }

        private void frmDangNhap_Load(object sender, EventArgs e)
        {
            LoadAllUI();
        }
        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            func_DangNhap();
        }
        private void btnCauHinh_Click(object sender, EventArgs e)
        {
            //this.Hide();
            //frmCauHinh frm = new frmCauHinh();
            //if (frm.ShowDialog() == DialogResult.Cancel)
            //{
            //    this.Show();
            //}


        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Enter:
                    btnDangNhap_Click(null, null);
                    return true;
                case Keys.F1:
                    btnCauHinh_Click(null, null);
                    return true;
                case Keys.Escape:
                    btnThoat_Click(null, null);
                    return true;
                case (Keys.F4 | Keys.Alt):
                    btnThoat_Click(null, null);
                    return true;

            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void SetErrMsg(string text)
        {
            //lblMsg.Text = text;
        }

        private void func_DangNhap()
        {
            if (txtNhanVien.Text == "")
            {
                SetErrMsg("Vui lòng nhập mã");
                txtNhanVien.Focus();
            }

            if (txtMatKhau.Text == "")
            {
                SetErrMsg("Vui lòng nhập mật khẩu");
                txtMatKhau.Focus();
            }

            //bool res = calamviecBLL.BatDauCaTruc(txtNhanVien.Text, txtMatKhau.Text);
            user user = userBLL.GetLogin(txtNhanVien.Text, txtMatKhau.Text);
            //if (!res)
            if(user == null)
            {
                //MessageBox.Show(ThongBao, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //SetErrMsg(calamviecBLL.ErrMsg);
                SetErrMsg("Tên đăng nhập hoặc mật khẩu không đúng");
                return;
            }
            RootStaticVar.CurrentUser = user;
            this.DialogResult = DialogResult.OK;
        }

        private void LoadAllUI()
        {
            SetErrMsg("");
            txtNhanVien.Focus();
        }
    }
}